<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;

use app\admin\model\Image;

use think\Request;
use think\Cache;
use think\exception\Handle;

/**
 * 信访人员
 */
class Visit extends Base
{

    public function __construct() {
        parent::__construct();
        $this->model = 'user';
        $this->addView = 'visit:add';
        $this->indexView = 'visit:list';
        $this->editView = 'visit:edit';

        $this->redirect = 'admin/visit/index';
        $this->searchFields = [
            'identityNumber' => [
                'label'     => '身份证',
                'field'     => 'identityNumber',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
            'p_phone' => [
                'label'     => '手机号',
                'field'     => 'p_phone',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];
    }

    /**
     * 列表
     */
    public function index() {
        $request = $this->request;
        $param = $request->param();
        // Reset filter

        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }

        if($request->isAjax()){
            $model = model($this->model);
            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);

            if($this->defaultWhere){
                //$model = $model->where($this->defaultWhere);
                $where = array_merge($this->defaultWhere, $where);
            }
            $where['status'] = 1;
            $where['type'] = 2;

            $list = $model->alias('a')
                ->where($where)
                ->limit($start, $length)
                ->order($order)
                ->select();

            foreach($list as $v)
            {
                if ($v['sex'] == 1){
                    $v['sex'] = '男';
                }else{
                    $v['sex'] = '女';
                }
                $v['location'] = $v['province'].'-'.$v['city'].'-'.$v['county'];
            }
            $count = $model->where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];
            return json($result);
        }

        return self::show($this->indexView, [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }

    /**
     * Show
     *
     * @param $template
     * @param array $data
     * @return mixed
     */
    private function show($template, $data = []) {

        return $this->view->fetch($template, $data);
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $model = model($this->model);
        $data = [
            'status' => 2
        ];
        $where = [
            'id' => $id
        ];
        $result = $model->update($data, $where,true);
        if($result !== false){
            return $this->success('删除成功',$this->redirect);
        }else{
            return $this->error('error !');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['identityNumber']) && $filter['identityNumber']){
                $where['identityNumber'] = ['like', '%'.$filter['identityNumber'].'%'];
            }
            if(isset($filter['p_phone']) && $filter['p_phone']){
                $where['p_phone'] = ['like', '%'.$filter['p_phone'].'%'];
            }
        }

        return $where;
    }

    /**
     * 下载Excel模板
     */
    public function downloadTemplate(){
        \app\admin\service\Personnel02::downloadTemplate($this->request);
    }

    /**
     * 导入数据
     */
    public function import(){
        $result = \app\admin\service\Personnel02::import();

        return $result;
    }

    /**
     * 导出数据
     */
    public function export(){
        $result = \app\admin\service\Personnel02::export($this->request);

        if($result['code'] == 2){
            $this->error($result['msg'], $this->redirect);
        }

        return redirect('admin/visit/index');
    }
}
